<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class District extends Model
{
    use HasFactory, SoftDeletes;

    const CITY_ID = 1;

    protected function reporter(): Attribute
    {
        return Attribute::make(
            get: fn() => User::where('district_id', $this->id)->whereHas('roles', function ($q) {$q->where('name', '填报单位填报员');})->get(),
        );
    }

    protected function reviewer(): Attribute
    {
        return Attribute::make(
            get: function () {
                if ($this->id == self::CITY_ID) {
                    $roleName = '市级审核员';
                } else {
                    $roleName = '填报单位审核员';
                }
                return User::where('district_id', $this->id)
                    ->whereHas('roles', function ($q) use ($roleName) {$q->where('name', $roleName);})
                    ->get();
            }
        );
    }

    public static function getCity()
    {
        return self::find(self::CITY_ID);
    }
}
